package cn.highedu.spring.bean17;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

/**
 * 显式定义Bean
 * 注意 不要将@PropertySource放到 BeanPostProcessor类上，否则无法读取到配置文件
 */
@Configuration
@PropertySource("classpath:/config/app.properties")
public class AppConfig {

    @Value("${max.retries}")
    private String maxRetries;

    public String getMaxRetries() {
        return maxRetries;
    }

    @Bean(initMethod = "init")
    public TransferService transferService(AccountRepository accountRepository) {
        TransferServiceImpl transferService = new TransferServiceImpl();
        transferService.setAccountRepository(accountRepository);
        return transferService;
    }

    @Bean
    public AccountRepository accountRepository(DataSource dataSource) {
        return new JdbcAccountRepository(dataSource);
    }

}

